package com.geekchique.runningbuddy.model;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Collection;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.geekchique.runningbuddy.activity.stopwatch.StopwatchTools;

import android.location.Location;
import android.net.ParseException;

public class ListOfRuns extends LinkedList<Run> {

	@Override
	public String[][] toArray() {
		int size = super.size();
		int runNo = 0;
		String[][] arrayOfJogs = new String[size][];
		StringBuffer coOrdinates;
		Iterator<Run> i = super.listIterator();
		DecimalFormat myFormatter = new DecimalFormat("0.000000");



		while (i.hasNext()) {
			Run run = (Run) i.next();
			String curr[] = new String[2];
			List<RunSegment> runSegments = run.getRunSegments();
			coOrdinates = new StringBuffer();

			curr[0] = new String (run.name);
			int noOfLocations = runSegments.size();
			int curentLoc = 0;

			for(RunSegment segment: runSegments){
				CheckPoint checkPoint = segment.getCheckPoint();
				Location location = checkPoint.getLocation();

				if (curentLoc == noOfLocations -1){
					coOrdinates.append(  myFormatter.format((location.getLongitude() / 1000000))  + "," + myFormatter.format((location.getLatitude()/ 1000000)) );
				}else{
					coOrdinates.append( myFormatter.format((location.getLongitude() / 1000000)) + "," + myFormatter.format((location.getLatitude()/ 1000000)) + ",");
				}
				curentLoc++;			
			}

			curr[1] = new String (coOrdinates);

			arrayOfJogs[runNo] = curr;
			runNo++;
		}

		return arrayOfJogs;
	}

	private static final String	DISPLAY_ZERO_TIME	= "00:00:00.00";

	@Override
	public void add(int arg0, Run arg1) {
		super.add(arg0, arg1);
	}

	@Override
	public boolean add(Run object) {
		return super.add(object);
	}

	@Override
	public boolean addAll(Collection<? extends Run> arg0) {
		return super.addAll(arg0);
	}

	@Override
	public boolean addAll(int arg0, Collection<? extends Run> arg1) {
		return super.addAll(arg0, arg1);
	}

	@Override
	public void clear() {
		super.clear();
	}

	private static final long	serialVersionUID	= 1234243236L;

	public long getTotalRunningTimeAsLong() {
		long totalTime = 0L;
		for (Run run : this) {
			totalTime = run.getTotalTime();
		}

		return totalTime;
	}

	public String getTotalRunningTimeForDisplay() {

		long totalRunTime = 0L;
		String result = DISPLAY_ZERO_TIME;

		if (this.size() > 0) {
			
			for (Run run : this) {
				totalRunTime = totalRunTime + run.getTotalTime();
			}

			try {
				result = StopwatchTools.getTimeInExpectedFormat(totalRunTime);

			} catch (ParseException e) {
				System.out.println("Totalling the date failed");
				e.printStackTrace();
			}
			return result;

		} else {
			return DISPLAY_ZERO_TIME;
		}

	}

}
